package com.haikesi.api.devicetype.controller;

import com.github.pagehelper.PageInfo;
import com.haikesi.api.base.ApiIndex;
import com.haikesi.api.base.Error;
import com.haikesi.api.base.R;
import com.haikesi.api.base.Type;
import com.haikesi.api.base.annotation.Token;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeSearchVO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeUpdateAlisaVO;
import com.haikesi.api.devicetype.domain.vo.DeviceTypeVO;
import com.haikesi.api.devicetype.service.IDeviceTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(description = "设备分类相关接口", tags = ApiIndex.DeviceTypeController)
@RequestMapping(value = "/api/deviceType")
@RestController
public class DeviceTypeController {

    @Autowired
    private IDeviceTypeService service;

    @ApiOperation(value = "查询设备分类列表")
    @GetMapping(value = "/list")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<PageInfo<List<DeviceTypeVO>>> list(
            @ApiParam(value = "查询参数") @ModelAttribute DeviceTypeSearchVO searchVO) {
        List<DeviceTypeVO> list = service.getList(searchVO);
        PageInfo pageInfo = new PageInfo(list);
        return new R(pageInfo);
    }

    @ApiOperation(value = "查询设备所有分类列表")
    @GetMapping(value = "/allList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R<List<DeviceTypeVO>> allList() {
        List<DeviceTypeVO> list = service.allList();
        return new R(list);
    }

    @ApiOperation(value = "更新本地数据库的设备类型")
    @PostMapping(value = "/updateList")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R list1(@ApiParam(value = "查询参数") @Valid @RequestBody String devicetypes) {
        service.updateType(devicetypes);
        return new R();
    }

    @ApiOperation(value = "修改设备类型别名")
    @PutMapping(value = "")
    @ApiImplicitParam(name = "token", value = "签名", paramType = "query", dataType = "String")
    @Token
    public R update(@ApiParam(value = "修改参数") @Valid @RequestBody DeviceTypeUpdateAlisaVO updateAlisaVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            List<Error> messages = new ArrayList<>();
            for (int i = 0; i < bindingResult.getFieldErrors().size(); i++) {
                messages.add(new Error(bindingResult.getFieldErrors().get(i).getField(), bindingResult.getFieldErrors().get(i).getDefaultMessage()));
            }
            return new R(Type.FORMAT_EXCEPTION).setMessages(messages);
        }
        service.update(updateAlisaVO);
        return new R();
    }

}
